package com.runlooker.runer.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.runlooker.runer.db.RunerDBOpenHelper;
import com.runlooker.runer.db.RunerTableSql;
import com.runlooker.runer.entity.PresenceEntity;

/**
 * 
 * 添加好友的申请记录
 * 
 * @author YINENG
 *
 */
public class PresenceDao {
	private final String tag = PresenceDao.class.getSimpleName();
	private String mTable = RunerTableSql.PRESENCE_ENTRY_TABLE;

	private RunerDBOpenHelper mRunerDBOpenHelper;

	public PresenceDao(Context context) {
		mRunerDBOpenHelper = new RunerDBOpenHelper(context);
	}

	public synchronized void saveOrUpdateEntity(PresenceEntity entity) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		if (isExited(entity, db)) {
			updatePresence(entity, db);
		} else {
			savePresence(entity, db);
		}
		// db.close();
	}

	private boolean isExited(PresenceEntity entity, SQLiteDatabase db) {
		boolean flag = false;
		Cursor cursor = db.query(mTable, null, "fromAccount = ? ",
				new String[] { entity.getFromAccount() }, null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			flag = true;
		} else {
			flag = false;
		}
		cursor.close();
		return flag;
	}

	public PresenceEntity getPresenceEntityByAccount(String account) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		PresenceEntity entity = new PresenceEntity();
		String selection = "fromAccount = ?";
		String[] selectionArgs = new String[] { account };
		Cursor cursor = db.query(mTable, null, selection, selectionArgs, null,
				null, null);
		if (cursor != null && cursor.getCount() > 0) {
			entity.setFromAccount(cursor.getString(cursor
					.getColumnIndex("fromAccount")));
			entity.setPrecenceType(cursor.getInt(cursor
					.getColumnIndex("precenceType")));
		} else {
			entity = null;
		}
		cursor.close();
		// db.close();
		return entity;
	}

	/**
	 * 
	 * 根据帐号，删除对应数据
	 * 
	 * @param account
	 */
	public synchronized void deleInfoByAccount(String account) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		String[] args = { account };
		db.delete(mTable, "fromAccount = ? ", args);
		// db.close();
	}

	/**
	 * 
	 * 清空表
	 * 
	 * @Title: clearTable
	 * @Description: 方法描述
	 */
	public synchronized void clearTable() {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		StringBuffer updateSql = new StringBuffer("DELETE FROM " + mTable);
		db.execSQL(updateSql.toString());
		// db.close();
	}

	private synchronized void savePresence(PresenceEntity entity,
			SQLiteDatabase db) {
		StringBuilder sql = new StringBuilder();
		sql.append("INSERT into ");
		sql.append(mTable + " (fromAccount,precenceType) values (");
		sql.append("'" + entity.getFromAccount() + "',");
		sql.append(entity.getPrecenceType() + ")");
		db.execSQL(sql.toString());
	}

	private synchronized void updatePresence(PresenceEntity entity,
			SQLiteDatabase db) {
		ContentValues cv = new ContentValues();
		cv.put("precenceType", entity.getPrecenceType());
		String[] args = { entity.getFromAccount() };
		db.update(mTable, cv, "fromAccount = ? ", args);
	}

	public synchronized List<Object> getListByPresenceType(
			int precenceType) {
		SQLiteDatabase db = mRunerDBOpenHelper.getReadableDatabase();
		List<Object> list = new ArrayList<Object>();
		String[] columns = new String[] { "fromAccount", "precenceType" };
		String selection = "precenceType = ?";
		String[] selectionArgs = new String[] { Long.toString(precenceType) };
		Cursor cursor = db.query(mTable, columns, selection, selectionArgs,
				null, null, null);
		if (cursor != null && cursor.getCount() > 0) {
			while (cursor.moveToNext()) {
				PresenceEntity entity = new PresenceEntity();
				entity.setFromAccount(cursor.getString(cursor
						.getColumnIndex("fromAccount")));
				entity.setPrecenceType(cursor.getInt(cursor
						.getColumnIndex("precenceType")));
				list.add(entity);
			}
		}
		cursor.close();
		// db.close();
		return list;
	}
}
